/* ===============================================================================
 * 版权          ：
 * 创建时间：2011-7
 * 作者          ：枢木 ideal35500@qq.com
 * 文件          ：wl.js
 * 功能          ：
 * 说明          ：
 * ===============================================================================
 */
/////////////////////////////////公共脚本/////////////////////////////////

function formatStr(){
    var ary = [];
    for (i = 1; i < arguments.length; i++) {
        ary.push(arguments[i]);
    }
    var s = "";
    return arguments[0].replace(/\{(\d+)\}/g, function(m, i){
        return ary[i];
    });
};
function g(objId){
    return document.getElementById(objId);
};
function setInnerHtml(objId, content){
    if (g(objId) != null) {
        g(objId).innerHTML = content;
    }
};
//设置是否可见
function setDisplay(objId, state){
    if (g(objId) != null) {
        g(objId).style.display = state;
    }
};
//显示等待信息
function showWaitInfo(obj, info){
    clearWaitInfo();
    createElement("div", "waitInfo", "waitInfo", null, info, obj);
};
//显示提示信息
function showAlert(obj, info, infoId){
    removeElement(infoId);
    createElement("div", infoId, "alertInfo", null, info, obj);
};
//显示提示信息（追加）
function showAlertAppend(obj, info, infoId){
    removeElement(infoId);
    createElement("span", infoId, null, "background-color:#FFECEC;border:1px solid #F34956;line-height:20px;color:#CC0000;", info, obj);
};
//创建元素
function createElement(element, elementId, className, cssText, innerHTML, parent){
    var d = window.document.createElement(element);
    if (elementId != null) {
        d.id = elementId;
    }
    if (className != null) {
        d.className = className;
    }
    if (cssText != null) {
        d.style.cssText = cssText;
    }
    if (innerHTML != null) {
        d.innerHTML = innerHTML;
    }
    if (parent != null) {
        parent.appendChild(d);
    }
    else {
        document.body.appendChild(d);
    }
};
//移除元素
function removeElement(elementId){
    if (g(elementId) != null) {
        g(elementId).parentNode.removeChild(g(elementId));
    }
};
function showMessageBoxBase(content, pos, width, msgId){
    removeElement(msgId);
    var dW = parseInt(document.documentElement.scrollWidth); //网页正文全文宽
    var dH = parseInt(document.documentElement.scrollHeight);//网页正文全文高
    var cssText = "";
    cssText = "left:" + (pos.left) + "px;";
    cssText += "top:" + pos.top + "px;position:absolute;width:" + width + "px;";
    createElement("div", msgId, null, cssText, content, null);
};
function showMessageBox(content, pos, width){
    showMessageBoxBase(content, pos, width, "MessageBox");
};

/////////////////////////////////公共脚本 END/////////////////////////////////

$(function(){
    //首页菜单项
    $('.bo').hover(function(){
		//$(this).children('a').addClass('act');
		//$('#slide_c').css({"visibility":"hidden"});
        $(this).find('.sub').slideDown('fast'); 
    }, function(){
		//$('#slide_c').css({"visibility":"visible"});
        $(this).find('.sub').hide();
        //$(this).children('a').removeClass('act');		
    });
    
    //热门图书 TAB
    var hotbooks_tab = $("#hotbooks h3 ul li");
    var hotbooks_tab_block = $("#hotbooks div ul");
    hotbooks_tab_block.hide().eq(0).show();
    $.each(hotbooks_tab, function(i, item){
        $(item).mouseover(function(){
            $(this).siblings().children("a").removeClass("selected");
            $("a", this).addClass("selected");
            hotbooks_tab_block.hide().eq(i).show();
        });
    });
    
    //首页图书、化妆品畅销榜 
    var channel_salewell = $(".channel_salewell");
    $.each(channel_salewell, function(i, item){
        var channel_salewell_li_even = channel_salewell.eq(i).find("li:even");
        var channel_salewell_li_odd = channel_salewell.eq(i).find("li:odd");
        channel_salewell_li_even.show().eq(0).hide();
        channel_salewell_li_odd.hide().eq(0).show();
        $.each(channel_salewell_li_even, function(i, item){
            $(item).mouseover(function(){
                channel_salewell_li_even.show().eq(i).hide();
                channel_salewell_li_odd.hide().stop().eq(i).show();
            });
        });
    });
});

function login(){
    window.location.href = "/Passport/Login.aspx?returl=" + escape(window.location.href);
};

function AddToCart(productId) {
    addToCart(productId, 1);
}

function addToCart(productId, buynum){
    if (buynum == null || parseInt(buynum) < 1) {
        buynum = 1;
    }
    
    var popup = window.open("/Ajax/Orders/ShoppingCartAdd.ashx?returl=" + escape(window.location.href) + "&productId=" + productId + "&buyNum=" + buynum, "wlcart", "");
    popup.focus();
};

/////////////////////////////////商品/////////////////////////////////

var ajax_product_service = "/Ajax/Products/product.ashx";

function addFav(productIds){
    var obj = null;
    if (arguments[1] != null) {
        obj = g(arguments[1]);
    }
    
    Ajax.call("/Ajax/Products/product_favority.ashx", "productIds=" + productIds, function(result, resultText){
        if (resultText.indexOf("登录") > 0) {
            location = "/Passport/Login.aspx?returl=" + escape(location.href);
        }
        else {
            if (resultText.indexOf("成功") > 0) {
                resultText = "已成功加入收藏";
            }
            else 
                if (resultText.indexOf("失败") > 0) {
                    resultText = "加入收藏失败";
                }
            
            if (obj != null) {
                var box = "";
                box += "<div class='modal mdtip'>";
                box += "	<h2><a href='javascript:;' onclick='removeElement(&quot;MessageBox&quot;)'></a></h2>";
                box += "	<div class='mdcon'>";
                box += "		<div class='layout'>";
                box += "			<img alt='' class='l' src='/images/logsuc.gif'>";
                box += "			<h4>{0}</h4>";
                box += "			<p><a href='/UserCenter/Transaction/Favorities/' target='_blank'>查看我的收藏&gt;&gt;</a></p>";
                box += "		</div>";
                box += "	</div>";
                box += "</div>";
                
                box = formatStr(box, resultText);
                
                var pos = Util.getPos(obj);
                pos.top = pos.top - 125;
                pos.left = pos.left - 400;
                
                showMessageBox(box, pos, 359);
            }
            else {
                alert(resultText);
            }
        }
    }, "GET", "TEXT", true);
};
function voteForComment(remarkId, agree){
    Ajax.call(ajax_product_service, "method=VoteForComment&remarkId=" + remarkId + "&agree=" + agree, function(result, resultText){
        voteForCommentResponse(parseInt(result), remarkId, agree);
    }, "GET", "TEXT", true);
};
function voteForCommentResponse(retCode, remarkId, agree){
    if (retCode == -1) {
        alert("请先登录");
        location = "/Passport/Login.aspx?returl=" + escape(location.href);
    }
    else 
        if (retCode == 0) {
            alert("感谢您的参与，评论成功");
            var obj;
            if (agree == 1) {
                obj = $("#comment_usefull" + remarkId);
            }
            else {
                agree = 1;
                obj = $("#comment_no_usefull" + remarkId);
            }
            obj.text(parseInt(obj.text()) + agree);
        }
        else 
            if (retCode == 1) {
                alert("您已经评论过了，不能重复评论");
            }
};
function addViewHistory(productId, productName){
    Ajax.call(ajax_product_service, "method=ViewHistory&productId=" + productId + "&productName=" + productName + "&handle=add", null, "GET", "TEXT", true);
};
function ViewHistory(){
    $("#viewHistory").children("div").html("<img src=\"../images/loading3.gif\" /> 内容加载中，请稍后...  ");
    Ajax.call(ajax_product_service, "method=ViewHistory", function(result, resultText){
        if (result.len > 0) {
            var div = "";
            $.each(result.item, function(i, view){
                //if (i > 0) {
                div += "<p class=\"layout\"><a href='/" + view.id + "' target='_blank'><img width='75' src='http://image.welan.com/?" + view.id + "' class='l' alt=''></a>";
                div += "   <a href='/" + view.id + "' target='_blank'>" + view.name + "</a><br />";
                div += "   <span class=\"lh\">&nbsp;</span>";
                div += "</p>";
                //}
            });
            $("#viewHistory").children("div").html(div);
        }
        else {
            $("#viewHistory").hide();
        }
    }, "GET", "JSON", true);
};
/////////////////////////////////商品 END/////////////////////////////////


